package nccloud.web.hrppl.pjapplication.pjappl.action;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

import nccloud.web.codeplatform.framework.action.base.BaseAction;
import nccloud.web.codeplatform.framework.action.base.RequstParamWapper;
import nccloud.web.codeplatform.framework.action.base.VOTransform;
import nccloud.web.codeplatform.framework.action.base.RequestDTO;

import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;

import nccloud.pubitf.platform.query.INCCloudQueryService;
import nccloud.dto.baseapp.querytree.dataformat.QueryTreeFormatVO;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nccloud.commons.lang.StringUtils;
import nccloud.commons.lang.ArrayUtils;
import nccloud.framework.web.ui.model.PageInfo;
import nc.itf.hrppl.pjapplication.pjappl.IPJApplHVOService;
import nc.vo.hrppl.pjapplication.PJApplHVO;
/**
 * 列表查询
 */
public class ListPJApplHVOAction extends BaseAction{

	@Override
	public Object doAction(IRequest request, RequstParamWapper paramWapper) throws Throwable{
		//json数据转换
		RequestDTO param = VOTransform.toVO(paramWapper.requestString,RequestDTO.class);
		
		PageInfo pageInfo = param.getPageInfo();
		
		//查询方案
		INCCloudQueryService nccloudService = ServiceLocator.find(INCCloudQueryService.class);
		QueryTreeFormatVO queryVo = param.getQueryTreeFormatVO();
		String searchSql = "";
		if(queryVo != null){
			IQueryScheme searchService = nccloudService.convertCondition(queryVo);
			searchSql = searchService.getWhereSQLOnly();
		}

		IPJApplHVOService service = ServiceLocator.find(IPJApplHVOService.class);
		//查询当前方案下所有的数据pk
		String[] allpks = service.listPJApplHVOPkByCond(searchSql);
		if(ArrayUtils.isEmpty(allpks)){
			return null;
		}
		//pk排序
		param.setAllpks(allpks);
		//根据分页拿到当前分页的pk
		String[] curPagePks = pageInfo == null ? allpks : paramWapper.pageResult(pageInfo, allpks);
		PJApplHVO[] vos =  service.listPJApplHVOByPk(curPagePks);

		Arrays.sort(vos, new Comparator<PJApplHVO>() {
			@Override
			public int compare(PJApplHVO o1, PJApplHVO o2) {
				int pk_orgCompare = o1.getOfficedept().compareTo(o2.getOfficedept());
				if (pk_orgCompare != 0) {
					return pk_orgCompare;
				}

				int belongproCompare = (o1.getBelongpro() == null ? "" : o1.getBelongpro()).compareTo(
						o2.getBelongpro() == null ? "" : o2.getBelongpro());
				if (belongproCompare != 0) {
					return belongproCompare;
				}

				// 处理def10，null视为较大，数字按大小排序
				Double def3_o1 = o1.getDef3() == null ? Double.MAX_VALUE : Double.parseDouble(o1.getDef3());
				Double def3_o2 = o2.getDef3() == null ? Double.MAX_VALUE : Double.parseDouble(o2.getDef3());
				return Double.compare(def3_o1, def3_o2);
			}
		});






		//排序会被打乱,重新再排一次序 申请日期
	//	nc.vo.trade.voutils.VOUtil.descSort(vos, new String[] {PJApplHVO.APPLICATIONDATE} );
		//构造返回结果
		return buildResult(param,false,null,vos);
	}
	
}